<?php

namespace app\xwam\controller;

/**
 * 备份还原控制器
 *
 * @version 1.0 2018-2-2 sochishun Added.
 */
class BackupController extends AppbaseController {

    // 需要把mysql/bin目录添加到全局变量,否则就必需指定mysqldump全路径
    public function index() {
        $list = [];
        $backuproot = env('app_path') . 'public' . DS . 'backup' . DS;
        $file = new \FilesystemIterator($backuproot);
        $filename = '';
        foreach ($file as $fileinfo) {
            $filename = $fileinfo->getFilename();
            $list[] = [
                'filename' => substr($filename, strpos($filename, '-') + 1, -4),
                'filepath' => $fileinfo->getPath() . DS . $fileinfo->getFilename(),
                'ctime' => date('Y-m-d H:i', $fileinfo->getCTime()),
                'filesize' => format_bytes($fileinfo->getSize()),
            ];
        }
        $this->assign('list', $list);
        return view();
    }

    /**
     * 数据库备份
     * @param type $type
     * @version 2018-2-2
     */
    public function dobackup($type = '') {
        $dbname = config('database.database');
        $username = config('database.username');
        $password = config('database.password');
        $backuproot = env('app_path') . 'public' . DS . 'backup' . DS;
        if (!is_dir($backuproot)) {
            try {
                mkdir($backuproot);
            } catch (\Exception $ex) {
                echo $ex->getMessage();
                exit;
            }
        }
        $timestamp = time();
        switch ($type) {
            case 'schema':
                $cmd = "-d --add-drop-table -R $dbname > {$backuproot}{$timestamp}-$dbname-schema.sql";
                break;
            case 'data':
                $cmd = "-t $dbname > {$backuproot}{$timestamp}-$dbname-data.sql";
                break;
            default:
                $cmd = "--add-drop-table -R $dbname > {$backuproot}{$timestamp}-$dbname-full.sql";
                break;
        }
        $cmd = "mysqldump -u$username -p$password $cmd";
        $result = exec($cmd, $output, $return_var);
        if ($return_var > 0) {
            echo '出错啦';
        } else {
            echo '成功';
        }
        var_export(compact('cmd', 'result', 'output', 'return_var'));
    }

    /**
     * 数据库还原操作
     * @version 2018-2-2
     */
    public function doreback($filepath = '') {
        $dbname = config('database.database');
        $username = config('database.username');
        $password = config('database.password');
        $backuproot = env('app_path') . 'public' . DS . 'backup' . DS;
        if (!is_dir($backuproot)) {
            echo '备份目录不存在';
            exit;
        }
        $cmd = "mysql -u$username -p$password $dbname < $filepath";
        $result = exec($cmd, $output, $return_var);
        if ($return_var > 0) {
            echo '出错啦';
        } else {
            echo '成功';
        }
        var_export(compact('cmd', 'result', 'output', 'return_var'));
    }

    public function delete($filepath = '') {
        if (!$filepath) {
            return json_encode(xsfn_message(false, '参数有误!'));
        }
        try {
            unlink($filepath);
            return json_encode(xsfn_message(true));
        } catch (\Exception $ex) {
            return json_encode(xsfn_message(FALSE, $ex->getMessage()));
        }
    }

}
